/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.nodes;

import com.android1.amarena2d.engine.GL;
import com.android1.amarena2d.engine.Resource;
import com.android1.amarena2d.engine.SpriteBatchInstance;
import com.android1.amarena2d.engine.render.AmarenaSpriteBatch;
import com.android1.amarena2d.texture.ManagedBitmapFont;

public class FontNode extends BaseNode<TextLabel> implements Resource.Consumer {

    ManagedBitmapFont managedBitmapFont;
    AmarenaSpriteBatch spriteBatch;

    public FontNode(ManagedBitmapFont managedBitmapFont) {
        this.managedBitmapFont = managedBitmapFont;
    }


    @Override
    public void init() {
        if (!init) {
            this.spriteBatch = SpriteBatchInstance.get();
            this.managedBitmapFont.obtainRef(this);
            this.managedBitmapFont.init();
            super.init();
        }
    }

    @Override
    public void dispose() {
        if (init) {
            this.managedBitmapFont.releaseRef(this);
            this.managedBitmapFont.dispose();
            super.dispose();
        }

    }

    @Override
    public void render() {
        if (!init || !active || !visible)
            return;
        if (Scene.CULLING && !this.disableCulling && !this.isVisibleOnScreen())
            return;

        final float x = getX();
        final float y = getY();

        final boolean isTranslate = transformDelegate != null || x != 0 || y != 0;

        if (isTranslate)
            GL.pushMatrix();

        GL.setColor(this.color);

        if (x != 0 || y != 0)
            GL.translate(x, y);

        if (transformDelegate != null) {
            transformDelegate.apply();
        }

        if (children != null) {
//            GL.blendFunc(BlendFunction.TEXT);
            spriteBatch.begin();
            onRender();
            renderChilds();
            onRenderEnd();
            spriteBatch.end();
//            GL.blendFunc(BlendFunction.DEFAULT);
        }

        if (isTranslate)
            GL.popMatrix();


    }


    public TextLabel createLabel() {
        TextLabel entity = new TextLabel();
        add(entity);
        return entity;
    }

    @Override
    public void add(TextLabel e) {
        super.add(e);
    }


    @Override
    public void setRenderChildsFirst(boolean renderChildsFirst) {
        throw new UnsupportedOperationException();
    }
}